package so.shanku.essential.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import aym.util.getservicesdata.GetServicesDataQueue;
import aym.util.getservicesdata.GetServicesDataUtil;
import net.tsz.afinal.annotation.view.ViewInject;

import so.shanku.essential.R;
import so.shanku.essential.activity.base.BaseActivity;
import so.shanku.essential.getdata.GetDataConfing;
import so.shanku.essential.getdata.GetDataQueue;
import so.shanku.essential.getdata.ShowGetDataError;
import so.shanku.essential.utils.ExtraKeys;
import so.shanku.essential.utils.StringUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2015/9/26.
 */
public class ForgetPasswordFirstActivity extends BaseActivity {
    @ViewInject(id=R.id.fill_view,click = "fill_view_click")
    private LinearLayout fill_view;
	/**
	 * 手机号或邮箱
	 */
	@ViewInject(id = R.id.et_phone_or_email)
	private EditText et_phone_or_email;

	/**
	 * 下一步
	 *
	 * @param savedInstanceState
	 */
	@ViewInject(id = R.id.btn_forgetpassword_first_next, click = "forgetpassword_first_next_click")
	private TextView btn_forgetpassword_first_next;
	private String phone_or_email;//获取输入框中的内容

    /**
     * 空白处 结束
     * @param view
     */
    public void fill_view_click(View view){
        finish();
    }
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_forget_password_first);
	}

	public void forgetpassword_first_next_click(View view) {
		phone_or_email = et_phone_or_email.getText().toString();
		if (!(StringUtil.isMobileNO(phone_or_email) || StringUtil.isEmail(phone_or_email))) {
			toast.showToast(R.string.phone_or_email_style_error);
			return;
		}

		loadingToast.show();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(GetDataQueue.Params_key, GetDataConfing.Key_Str);

		params.put("PhoneOrEmail", phone_or_email);
		GetDataQueue queue = new GetDataQueue();
		queue.setActionName(GetDataConfing.Action_GetSendPhoneOrEmailOne);
		queue.setWhat(GetDataConfing.What_GetSendPhoneOrEmailOne);
		queue.setParamsNoJson(params);
		queue.setCallBack(callBack);
		getDataUtil.getData(queue);

	}
	private GetServicesDataUtil.IGetServicesDataCallBack callBack = new GetServicesDataUtil.IGetServicesDataCallBack() {

		@Override
		public void onLoaded(GetServicesDataQueue entity) {
			if (entity.isOk()) {
				if (ShowGetDataError.isOkAndCodeIsNot1(mContext, entity.getInfo())) {
					 if(StringUtil.isEmail(phone_or_email)) {
						 Intent intent = new Intent(mContext, ForgetPasswordThirdActivity.class);
						 intent.putExtra(ExtraKeys.Key_Msg1, phone_or_email);
						 startActivityForResult(intent, 1);
					 } else {
						 Intent intent = new Intent(mContext, ForgetPasswordSecondActivity.class);
						 intent.putExtra(ExtraKeys.Key_Msg1, phone_or_email);
						 startActivityForResult(intent, 1);
					 }
				}
			} else {
				ShowGetDataError.showNetError(mContext);
			}
			loadingToast.dismiss();
		}
	};

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			setResult(RESULT_OK);
			finish();
		}
	}
}